<!--
 * @author gaohailan
 -->
<template>
  <div class="app-container reason-list">
  <el-collapse-transition>
      <div v-show="searchshow" class="common-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model.trim="listQuery.name"
          clearable
          placeholder="按改善名称搜索"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <div class="common-search-btn">
          <el-button
            class="filter-item"
            type="primary"
            @click="handleFilter"
          >{{ $t('table.search') }}</el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn">重置</el-button>
          <span class="common-search-more" @click="searchshow = !searchshow">
            更多筛选条件
            <i class="el-icon-arrow-down" />
          </span>
        </div>
      </div>
    </el-collapse-transition>
    <el-collapse-transition>
      <div v-show="!searchshow" class="common-search separate">
        <div class="common-search-inner">
          <el-row :gutter="20">
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">提交人</div>
                <el-input
                  v-model.trim="search.userName"
                  clearable
                  class="filter-item"
                  style="width: 100%;"
                  placeholder="请输入提交人搜索"
                />
              </div>
            </el-col>
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">联系电话</div>
                <el-input
                  v-model.trim="search.phoneNumber"
                  clearable
                  class="filter-item"
                  style="width: 100%;"
                  placeholder="请输入联系电话搜索"
                />
              </div>
            </el-col>
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">状态</div>
                <el-select
                  v-model="search.acceptState"
                  clearable
                  style="width: 90%;"
                  placeholder="请选择受理状态"
                >
                  <el-option
                    v-for="item in stateList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">提交时间</div>
                <el-date-picker
                  v-model="search.time"
                  style="width: 100%;"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="提交日期"
                />
              </div>
            </el-col>
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">改善名称</div>
                <el-input
                  v-model.trim="search.name"
                  clearable
                  class="filter-item"
                  style="width: 100%;"
                  placeholder="请输入改善名称搜索"
                />
              </div>
            </el-col>
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">建议类型</div>
                <el-select
                  v-model="search.category"
                  clearable
                  style="width: 90%;"
                  placeholder="请选择建议类型"
                >
                  <el-option
                    v-for="item in typeList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </div>
            </el-col>
          </el-row>
        </div>
        <div class="common-search-btn">
          <el-button type="primary" @click="handleFilter">{{ $t('table.search') }}</el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn">重置</el-button>
          <span class="common-search-more" @click="searchshow = !searchshow">
            精简筛选条件
            <i class="el-icon-arrow-up" />
          </span>
        </div>
      </div>
    </el-collapse-transition>
    <div class="common-btn">
      <div class="btn-title">
        <span>合理化改善列表</span>
      </div>
      <!-- <div class="btn-btns">
        <el-button style="margin-left: 10px;" @click="handleCreate">新建活动</el-button>
        <el-button style="margin-left: 10px;" @click="handleDeletes">批量删除</el-button>
      </div> -->
    </div>
    <div class="table">
      <el-table
        v-loading="listLoading"
        :data="honorList"
        class="tableScope"
        :header-cell-style="rowClass"
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="name" label="改善名称" show-overflow-tooltip/>
        <el-table-column prop="userName" label="提交人" show-overflow-tooltip/>
        <el-table-column prop="jobs" label="所在单位及部门" show-overflow-tooltip/>
        <el-table-column prop="advicePeople" label="建议人" show-overflow-tooltip/>
        <el-table-column prop="category" label="建议类型" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.category==1 ? '生产经营' : scope.row.category==2 ? '管理':scope.row.category==3 ? '安全':scope.row.category==4 ? '职工教育':scope.row.category==5 ? '党建及精神文明建设':'生活福利' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="phoneNumber" label="联系电话" show-overflow-tooltip/>
        <el-table-column prop="crtTime" label="提交时间" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.crtTime ? scope.row.crtTime.substring(0,10) : '' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="acceptPerson" label="受理人" show-overflow-tooltip/>
        <el-table-column prop="publicity" label="是否公示" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.publicity==0 ? '公示':'不公示' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="acceptState" label="状态" show-overflow-tooltip width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.acceptState==0 ? '待二级单位受理' : scope.row.acceptState==1 ? '待集团受理':scope.row.acceptState==2 ? '已受理待确认':scope.row.acceptState==3 ? '受理完成':scope.row.acceptState==4 ?'不通过':scope.row.acceptState==5 ?'待普通用户受理':'' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!-- 集团受理状态（0,带二级单位受理，1,带集团受理，2,已受理待确认，3,受理完成，4,不通过 5,待普通用户受理）acceptState -->
            <el-button v-if="userType == '平台管理员' && (scope.row.acceptState ==1 || scope.row.acceptState ==2)" type="text" size="mini" @click="handleUpdate(scope.row,'one')">受理</el-button>
            <!-- 二级  -->
            <el-button v-if="userType == '二级单位管理员' && scope.row.acceptState ==0" type="text" size="mini" @click="handleUpdate(scope.row,'two')">受理</el-button>


            <!-- 集团--->
            <el-button v-if="userType == '平台管理员' && (scope.row.acceptState == 0 || scope.row.acceptState ==3  || scope.row.acceptState == 4 || scope.row.acceptState == 5)" type="text" size="mini" @click="handelLook(scope.row)">查看</el-button>
            <!-- 二级  -->
            <el-button v-if="userType == '二级单位管理员' && (scope.row.acceptState == 1 || scope.row.acceptState == 2  || scope.row.acceptState == 3 || scope.row.acceptState == 4 || scope.row.acceptState == 5)" type="text" size="mini" @click="handelLook(scope.row)">查看</el-button>

            <!-- 集团--->
            <el-button v-if="userType == '平台管理员' && (scope.row.acceptState == 0 || scope.row.acceptState ==3  || scope.row.acceptState == 4 || scope.row.acceptState == 5)"  type="text" size="mini" @click="download(scope.row)">下载</el-button>
            <!-- 二级  -->
            <el-button v-if="userType == '二级单位管理员' && (scope.row.acceptState == 2  || scope.row.acceptState == 3 || scope.row.acceptState == 4 || scope.row.acceptState == 5)" type="text" size="mini" @click="download(scope.row)">下载</el-button>

          </template>
        </el-table-column>
      </el-table>
    </div>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList"/>
  </div>
</template>
<script>
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
  // import { rationalizationAdvice, SuggestActiveDelete } from '@/api/reasonable/index'
  export default {
    name: 'improveIndex',
    components: { Pagination },
    data() {
      return {
        userType:localStorage.getItem('userTypeRemark'),
        listLoading: false,
        total: 0,
        listQuery: {
          page: 1,
          limit: 10,
          name: ''
        },
        multipleSelection: [],
        honorList: [],
        search:{
          phoneNumber:"",
          acceptState:"",
          time:"",
          name:"",
          userName:"",
          category:"",
        },
        searchshow: true,
        typeList:[
          {id:1,name:'生产经营'},
          {id:2,name:'管理'},
          {id:3,name:'安全'},
          {id:4,name:'职工教育'},
          {id:5,name:'党建及精神文明建设'},
          {id:6,name:'生活福利'},
        ],
        stateList:[
          {id:0,name:'待二级单位受理'},
          {id:1,name:'待集团受理'},
          {id:2,name:'已受理待确认'},
          {id:4,name:'不通过'},
          {id:3,name:'受理完成'},
        ],
      }
    },
    created() {
      // this.getList()
    },
    methods: {
      download(row){
        window.open('/API-BACKEND/admin/rationalizationAdvice/rationalizationAdvice?id=' + row.id)
      },
      selectionFn(item){
        this.$router.push({path: '/reasonable/reasonableAnswer', name: 'ReasonableAnswer', query: {id: item.id,rowData:item}})
      },
      rowClass({ row, rowIndex }) {
        return {
          padding: '5px 0',
          height: '20px',
          background: 'rgba(247,247,248,1)'
        }
      },
      handleSelectionChange(arr) {
        this.multipleSelection = arr
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      resettingBtn(){
        this.listQuery.name = ''
        this.search.name = ''
        this.search.phoneNumber = ''
        this.search.acceptState = ''
        this.search.time = ''
        this.search.category = ''
        this.search.userName = ''
        this.getList()
      },
      handleCreate() {
        this.$router.push({path: '/reasonable/edit', name: 'ReasonableEdit'})
      },
      handleUpdate(item,val) {
        var ths = this
        localStorage.setItem('rowData',JSON.stringify(item))
        if (val == 'one' ) {
          if(item.acceptState == 1){//待集团受理
            ths.$router.push({path: '/improveEdit', name: 'improveEdit', query: {id: item.id,acceptState:item.acceptState,checkType:val}})
          }else{//待集团确认
            ths.$router.push({path: '/improves', name: 'improves', query: {id: item.id,a:0,acceptState:item.acceptState}})
          }
        }else{
          ths.$router.push({path: '/improveEdit', name: 'improveEdit', query: {id: item.id,acceptState:item.acceptState,checkType:val}})
        }
      },
      handelLook(item){
        localStorage.setItem('rowData',JSON.stringify(item))
        this.$router.push({path: '/improves', name: 'improves', query: {id: item.id,a:1}})
      },
      handelDel(row) {
        this.deleteHonor(row.id)
      },
      handleDeletes() {
        const arr = []
        if (this.multipleSelection.length > 0) {
          this.multipleSelection.forEach(item => {
            arr.push(item.id)
          })
          var ids = arr.join(',')
          this.deleteHonor(ids)
        } else {
          this.$message({
            message: '请选择需要删除的数据！',
            type: 'warning'
          })
        }
      },
      getList(obj) {
        this.listLoading = true
        var query = {
          name: this.listQuery.name || this.search.name,
          phoneNumber:this.search.phoneNumber,
          acceptState:this.search.acceptState,
          category:this.search.category,
          crtTime:this.search.time,
          userName:this.search.userName,
          page: obj ? obj.page : this.listQuery.page,
          pageSize: obj ? obj.limit : this.listQuery.limit
        }
        return new Promise((resolve, reject) => {
          rationalizationAdvice(query).then(response => {
            this.honorList = response.data.data.rows
            this.total = response.data.data.total
            setTimeout(() => {
              this.listLoading = false
            }, 1 * 1000)
          }).catch(error => {
            reject(error)
            setTimeout(() => {
              this.listLoading = false
            }, 1 * 1000)
          })
        })
      },
      deleteHonor(ids) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return new Promise((resolve, reject) => {
            SuggestActiveDelete({ids:ids}).then(() => {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            }).catch(error => {
              reject(error)
            })
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })

      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss">
  $dark_gray: #bfced6;
  $dark_main: #18A5FD;
  
  .reason-list {
    
  .common-search {
    background-color: #fff;
    padding: 0 20px;
    .svg-container {
      padding: 0;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: "";
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .common-search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .common-search-more {
        font-size: 14px;
        font-weight: 500;
        color: $dark_main;
        cursor: pointer;
        margin-left: 10px;
      }
    }
    &.merge {
      height: 50px;
      line-height: 50px;
    }
    &.separate {
      .common-search-inner {
        padding-top: 10px;
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }
          .el-input {
            position: relative;
            &.el-date-editor .el-input__inner {
              padding-left: 30px;
            }
            .el-input__inner {
              border: none;
              padding: 0;
            }
            &:after {
              content: "";
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .common-search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }
    .common-btn {
      display: flex;
      height: 36px;
      line-height: 36px;
      margin-bottom: 10px;
      .btn-title {
        flex: 1;
        font-size: 16px;
        font-weight: bold;
        font-family: PingFangSC-Medium;
        color: rgba(51, 51, 51, 1);
      }
    }
    .el-dialog {
      .el-dialog__body {
        padding: 20px;
      }
      .el-dialog__footer {
        border-top: 1px solid #E2E3E8;
        padding: 15px 20px;
      }
    }
  }
</style>
